package com.sfh.cpc.com.ui;

import android.text.TextUtils;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.server.IUserServer;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.lib.ui.dialog.DialogBuilder;

/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @date 2019/5/23
 */
public class VipLimit {

    public static boolean vip(){
        final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (user != null && user.isVip()){
            return true;
        }else {
            return false;
        }
    }
    /***
     * 帖子鉴权检查
     * @param note
     * @return
     */
    public static boolean lookNote(ENote note) {

        if (TextUtils.equals("1", note.getNoteVip())) {
            final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
            if (user == null || user.getUserId() <= 0) {
                return false;
            }

            if (note.getUserId() != user.getUserId() && !user.isVip()) {
                return false;
            }
        }
        return true;
    }

    /***
     * 查看社团鉴权检查
     * @param mass
     * @return
     */
    public static boolean lookMass(EMass mass) {
        if (TextUtils.equals("1", mass.getNoteVip())) {
            final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
            if (user == null || user.getUserId() <= 0) {
                return false;
            }
            if (mass.getMassUserId() != user.getUserId() && !user.isVip()) {
                return false;
            }
        }
        return true;
    }

    /***
     * 帖子鉴权检查
     * @param fragment
     * @param note
     * @return
     */
    public static boolean lookNote(final AbstractLifecycleActivity fragment, ENote note) {

        if (TextUtils.equals("1", note.getNoteVip())) {
            final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
            if (user == null || user.getUserId() <= 0) {
                //要求登录
                DialogBuilder builder = new DialogBuilder();
                builder.setMessage("请登录,去登录");
                builder.setHideCancel(true);
                builder.setOKTextListener("登录", new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                        dialog.dismiss();
                        IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                        if (userServer != null) {
                            userServer.openLogin(fragment);
                        }
                    }
                });
                fragment.showDialog(builder);
                return false;
            }

            if (note.getUserId() != user.getUserId() && !user.isVip()) {
                //要求VIP
                DialogBuilder builder = new DialogBuilder();
                builder.setMessage("需要VIP限制,激活VIP");
                builder.setHideCancel(true);
                builder.setOKTextListener("激活", new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                        dialog.dismiss();
                        IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                        if (userServer != null) {
                            userServer.openVip(fragment,note.getNoteId(),1);
                        }
                    }
                });
                fragment.showDialog(builder);
                return false;
            }
        }
        return true;
    }

    /***
     * 查看社团鉴权检查
     * @param fragment
     * @param mass
     * @return
     */
    public static boolean lookMass(final AbstractLifecycleActivity fragment, EMass mass) {
        if (TextUtils.equals("1", mass.getNoteVip())) {
            final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
            if (user == null || user.getUserId() <= 0) {
                //要求登录
                DialogBuilder builder = new DialogBuilder();
                builder.setMessage("请登录,去登录");
                builder.setHideCancel(true);
                builder.setOKTextListener("登录", new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                        dialog.dismiss();
                        IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                        if (userServer != null) {
                            userServer.openLogin(fragment);
                        }
                    }
                });
                fragment.showDialog(builder);
                return false;
            }

            if (mass.getMassUserId() != user.getUserId() && !user.isVip()) {
                //要求VIP
                DialogBuilder builder = new DialogBuilder();
                builder.setMessage("需要VIP限制,激活VIP");
                builder.setHideCancel(true);
                builder.setOKTextListener("激活", new DialogBuilder.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                        dialog.dismiss();
                        IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                        if (userServer != null) {
                            userServer.openVip(fragment,mass.getMassId(),2);
                        }
                    }
                });
                fragment.showDialog(builder);
                return false;
            }
        }
        return true;
    }

}
